using System.Collections;
using UnityEngine;

public class MissionHUD : MonoBehaviour
{
	public WindowNewMission windowNewMission;

	public MissionNote missionNote;

	public WindowCompleteMissionWithMsg completeMissionMsg;

	public WindowLostMissionMsg lostMissionMsg;

	public MessageBox msgBox;

	private void Awake()
	{
		ManagerMissions.startMission += ShowNote;
		ManagerMissions.completeMission += HideNote;
		ManagerMissions.stopMission += HideNote;
		missionNote.gameObject.SetActive(true);
	}

	public void ShowNewMissionWindow(MissionBase mission)
	{
		StartCoroutine(ShowNewMissionWindowIE(mission));
	}

	private IEnumerator ShowNewMissionWindowIE(MissionBase mission)
	{
		while (GameController.instance.playerScript.isDead)
		{
			yield return null;
		}
		if (windowNewMission == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			ResourceRequest complete = Resources.LoadAsync("MyMissions/StartMission");
			yield return complete;
			GameObject tmp = Object.Instantiate(complete.asset as GameObject);
			tmp.transform.parent = base.transform;
			tmp.transform.localScale = Vector3.one;
			windowNewMission = tmp.GetComponent<WindowNewMission>();
			windowNewMission.gameObject.SetActive(true);
			windowNewMission.messageBox = msgBox;
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		windowNewMission.ShowWindow(true);
		windowNewMission.UpdateParametrMission(mission);
		SetZeroTimeScale();
	}

	private void SetZeroTimeScale()
	{
		Time.timeScale = 0f;
	}

	public void HideMissionWindow()
	{
		if (windowNewMission != null)
		{
			windowNewMission.ShowWindow(false);
		}
		Time.timeScale = 1f;
	}

	private IEnumerator ShowMissionFaledWindowIE(MissionBase mission)
	{
		while (GameController.instance.playerScript.isDead)
		{
			yield return null;
		}
		if (lostMissionMsg == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			ResourceRequest complete = Resources.LoadAsync("MyMissions/LostMission");
			yield return complete;
			GameObject tmp = Object.Instantiate(complete.asset as GameObject);
			tmp.transform.parent = base.transform;
			tmp.transform.localScale = Vector3.one;
			lostMissionMsg = tmp.GetComponent<WindowLostMissionMsg>();
			lostMissionMsg.gameObject.SetActive(true);
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		lostMissionMsg.ShowWindow(true);
		lostMissionMsg.UpdateParametrMission(mission);
		SetZeroTimeScale();
	}

	public void ShowMissionFaled(MissionBase mission)
	{
		StartCoroutine(ShowMissionFaledWindowIE(mission));
	}

	public void HideMissionFaled()
	{
		if (lostMissionMsg != null)
		{
			lostMissionMsg.ShowWindow(false);
		}
		Time.timeScale = 1f;
	}

	private IEnumerator ShowMissionCompleateWindowIE(MissionBase mission)
	{
		while (GameController.instance.playerScript.isDead)
		{
			yield return null;
		}
		if (completeMissionMsg == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			ResourceRequest complete = Resources.LoadAsync("MyMissions/EndMission");
			yield return complete;
			GameObject tmp = Object.Instantiate(complete.asset as GameObject);
			tmp.transform.parent = base.transform;
			tmp.transform.localScale = Vector3.one;
			completeMissionMsg = tmp.GetComponent<WindowCompleteMissionWithMsg>();
			completeMissionMsg.gameObject.SetActive(true);
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		completeMissionMsg.ShowWindow(true);
		completeMissionMsg.UpdateParametrMission(mission);
		SetZeroTimeScale();
	}

	public void ShowMissionCompleate(MissionBase mission)
	{
		StartCoroutine(ShowMissionCompleateWindowIE(mission));
	}

	public void HideMissionCompleate()
	{
		if (completeMissionMsg != null)
		{
			completeMissionMsg.ShowWindow(false);
		}
		Time.timeScale = 1f;
	}

	public void ShowNote()
	{
		IndicatorMissions.SetVisible(true);
		missionNote.Show();
	}

	public void HideNote()
	{
		IndicatorMissions.SetVisible(false);
		missionNote.Hide();
	}

	public void OnDestroy()
	{
		ManagerMissions.startMission -= ShowNote;
		ManagerMissions.completeMission -= HideNote;
		ManagerMissions.stopMission -= HideNote;
	}
}
